﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shell32;
using System.IO;

namespace VideoLibrary
{
    public class FileInfoClass
    {
        public FileInfoClass()
        {
        }

        #region 私有方法

        /// <summary>
        /// 获取文件的某个信息
        /// </summary>
        /// <param name="iColumn"></param>
        /// <returns></returns>
        private static string GetFileInfo(int iColumn, string fileName)
        {
            ShellClass sh = new ShellClass();
            Folder dir = sh.NameSpace(Path.GetDirectoryName(fileName));
            FolderItem item = dir.ParseName(Path.GetFileName(fileName));
            string detail = dir.GetDetailsOf(item, iColumn);
            return detail;
        }

        #endregion

        /// <summary>
        /// 返回Double类型的时间，次方法为静态方法
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static double GetSeconds(string length)
        {
            DateTime dateTime;
            if (DateTime.TryParse(length, out dateTime))
            {
                return dateTime.TimeOfDay.TotalSeconds;
            }
            return 0;
        }

        /// <summary>
        /// 根据目录路径获取音乐文件，次方法为静态方法
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<FileModel> GetFileModelList(string path)
        {
            List<FileModel> list = null;
            if (Directory.Exists(path))
            {
                list = new List<FileModel>();
                string[] files = Directory.GetFiles(path, "*.mp3", SearchOption.AllDirectories);
                foreach (string item in files)
                {
                    string name = GetFileInfo(0, item);
                    string author = GetFileInfo(20, item);
                    string time = GetFileInfo(27, item);
                    string type = GetFileInfo(2, item);//文件类型
                    string genre = GetFileInfo(16, item);//流派
                    list.Add(new FileModel()
                    {
                        Name = name,
                        Author = author,
                        Time = time,
                        Genre = genre,
                        Path = item
                    });
                }
            }
            return list;
        }
    }

    public class FileModel
    {
        public string Name { get; set; }
        public string Author { get; set; }
        public string Time { get; set; }
        public string Genre { get; set; }
        public string Path { get; set; }
    }
}
